#!/usr/bin/env bash
export SCRIPTPATH="$( cd "$(dirname "$0")" ; pwd -P )"


# [ COLOR SCHEME ] ###########################################################
# Available themes:
# - catppuccin
#
# NOTE: 
# To avoid issues colors.sh will check the theme name provided below and try
# to fallback to the amber theme if what you provide is NULL or does not exist
# in the array on line 12.
source $SCRIPTPATH/scripts/colors.sh catppuccin
##############################################################################


# [ BASIC ] ##################################################################
# down the last running theme
if [ -f "/tmp/leftwm-theme-down" ]; then
    /tmp/leftwm-theme-down
    rm /tmp/leftwm-theme-down
fi
ln -s $SCRIPTPATH/down /tmp/leftwm-theme-down

# set the theme.toml config
echo "LoadTheme $SCRIPTPATH/theme.toml" > $XDG_RUNTIME_DIR/leftwm/commands.pipe
##############################################################################




# [ RUN STUFF ] ##############################################################
# keybindings daemon (if it exist)
if [ -x "$(command -v sxhkd)" ]; then
    sxhkd -c $HOME/.config/leftwm/sxhkd/sxhkdrc &
fi

# apps
numlockx on &

# notifications
if [ -x "$(command -v dunst)" ]; then
  pkill dunst
  dunst -config $HOME/.config/leftwm/themes/current/conf/dunst.config &
fi
#/usr/lib/xfce4/notifyd/xfce4-notifyd &


# picom (if it exists)
if [ -x "$(command -v picom)" ]; then
  picom --config $HOME/.config/leftwm/themes/current/conf/picom.config &> /dev/null &
fi
##############################################################################




# [ WALLPAPERS ] #############################################################
# - Comment/uncomment the option you prefer

# 1) START - set random wallpapers -----------------------------------------+
if [ -x "$(command -v feh)" ]; then
  feh --randomize --bg-fill "$SCRIPTPATH"/wallpapers/*
fi
# END ----------------------------------------------------------------------+


# 2) START - set ONE wallpaper for all displays ----------------------------+
#if [ -x "$(command -v feh)" ]; then
#  feh --bg-fill "$SCRIPTPATH"/wallpapers/atlas-ark-gruvbox-simple-lines.png
#fi
# END ----------------------------------------------------------------------+


# 3) START - set MULTIPLE wallpapers for all displays ----------------------+
# NOTE: the last image path DO NOT end with "\"
#if [ -x "$(command -v feh)" ]; then
#  feh --bg-fill \
#  "$SCRIPTPATH"/wallpapers/blue_unicat.png \
#  "$SCRIPTPATH"/wallpapers/pink_unicat.png
#fi
# END ----------------------------------------------------------------------+
##############################################################################



# [ START STATUSBAR ] #########################################################
  pkill polybar &
  index=0
  monitor="$(polybar -m | grep +0+0 | sed s/:.*// | tac)"
  leftwm-state -q -n -t $SCRIPTPATH/sizes.liquid | sed -r '/^\s*$/d' | \
  while read -r width x y
  do 
    barname="mainbar$index"
    monitor="$(polybar -m | awk -v i="$(( index + 1 ))" 'NR==i{print}' | sed s/:.*// | tr -d '\n')"
    monitor=$monitor width=$(( width - 30 )) polybar -c "$SCRIPTPATH"/polybar.config $barname &
    let index=index+1
  done
  ;;
######################################################################
